import {
    _decorator,
    animation,
    Animation,
    AnimationClip,
    Component,
    Node,
    resources,
    Sprite,
    SpriteFrame,
    UITransform,
} from "cc";
import { TILE_HEIGHT, TILE_WIDTH } from "../Scene/TileManager";
import { CONTROLLER_ENUM, DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_STATE_ENUM, ENTITY_TYPE_ENUM, EVENT_TYPE, PARAMS_NAME_ENUM } from "../../Enums";
import { EntityManager } from "../../Base/EntityManager";
import DataManager from "../../Runtime/DataManager";
import EventManager from "../../Runtime/EventManager";
import { DoorStateMachine } from "./DoorStateMachine";
import { IEntity } from "../../Levels";
const { ccclass, property } = _decorator;

@ccclass("DoorManager")
export class DoorManager extends EntityManager {

    async init(params: IEntity) {
        this.fsm = this.addComponent(DoorStateMachine);
        await this.fsm.init();

        super.init(params);

        EventManager.Instance.on(EVENT_TYPE.OPEN_DOOR, this.handleOpenDoor, this);
    }

    protected onDestroy(): void {
        super.onDestroy();
        EventManager.Instance.off(EVENT_TYPE.OPEN_DOOR, this.handleOpenDoor);
    }

    handleOpenDoor() {
        if (
            DataManager.Instance.enemies.every(enemy => enemy.state === ENTITY_STATE_ENUM.DEATH) &&
            this.state !== ENTITY_STATE_ENUM.DEATH
        ) {
            this.state = ENTITY_STATE_ENUM.DEATH;
        }
    }

    update() {
        super.update();
    }
}
